<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">

        <el-form  style="display: inline-block;" :inline="true" :model="formData" class="demo-form-inline">
          <el-form-item label="标签名称" >
            <el-input v-model="formData.tagName" placeholder="标签名称"></el-input>
          </el-form-item>
          <el-form-item  label="状态">
              <el-select style="margin-right: 10px;" v-model="formData.state" placeholder="请选择">
                    <el-option
                      v-for="item in stateselect"
                      :key="item"
                      :value="item"
                      :label="formatstate(item)"
                    />
                  </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="purge">清除</el-button>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-form-item>
        </el-form>

        <div style="float: right;">
        <el-button
          v-if="$route.query.id"
          type="text"
          class="el-icon-back"
          @click="$router.push('/subjects/list')"
          >返回学科</el-button
        >

        <el-button type="success"  @click="addLabel">
          <i class="el-icon-edit"/>
          新增标签
          </el-button>
        </div>
          <el-alert
            type="info"
            show-icon
            class="alert"
            :closable="false"
            >
            数据一共{{total}}条
          </el-alert>

         <el-table
          :data="tableData"
          style="width: 100%">

          <el-table-column  fixed prop="date" label="序号" type="index" :index="indexMethod" ></el-table-column>
          <el-table-column  prop="subjectName" label="所属学科"></el-table-column>
          <el-table-column  prop="tagName" label="标签名称"></el-table-column>
          <el-table-column  prop="username" label="创建者"></el-table-column>
          <el-table-column  prop="addDate"   label="创建日期"></el-table-column>
          <el-table-column  class="state"  label="状态">
            <template #default="{row}">{{formatstate(row.state)}}</template>
          </el-table-column>
          <el-table-column fixed="right" label="操作">
            <template #default="{ row }">
              <el-button type="text" @click="changeState(row)">
                {{ row.state === 0 ? '启用' : '禁用' }}
              </el-button>
              <el-button :disabled="row.state === 0" type="text" size="small" @click="dellable(row)">删除</el-button>
              <el-button :disabled="row.state === 0" type="text" size="small" @click="reviselable(row.id)" >修改</el-button>
            </template>
          </el-table-column>

        </el-table>

        <div class="block">
        <el-row  type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
          :hide-on-single-page='this.total/this.params.page >1'
            background
            @current-change="currentChange"
            @size-change="sizeChange"
            :page-sizec="params.pagesize"
            :page-sizes="[5, 10, 20, 50]"
            :current-page.sync="params.page"
            :page-size="10"
            layout="prev, pager,next,sizes,jumper"
            :total="total*1">
          </el-pagination>
        </el-row>
        </div>
      </el-card>
      <!-- 新增 -->
      <tags-add
        ref="addRole"
        :dialogVisible="showdialog"
        :shujuLeix="shujuleix"
        :detailId="detailid"
        :reviseLables="reviselables"
        @close-dialog="closeDialog"
      />
    </div>
  </div>
</template>
<script>
import TagsAdd from './companys/tags-add'
import { list, remove, changeState } from '../../api/hmmm/tags'
export default {
  components: {
    TagsAdd
  },
  mounted () {
    this.fetchList()
  },
  data () {
    return {
      jumpid: {
        subjectID: ''
      },
      reviselables: {
        subjectID: '',
        tagName: ''
      },
      detailid: '',
      total: '',
      params: {
        page: '',
        pagesize: ''
      },
      formData: {
        state: null,
        tagName: ''
      },
      stateselect: [0, 1],
      shujuleix: [{}],
      showdialog: false,
      tableData: [{
        subjectName: '',
        tagName: '',
        username: '',
        addDate: '',
        totals: '',
        state: ''
      }],
      rules: {
        state: [
          { required: true, message: '请选择学科', trigger: 'blur' }
        ],
        tagName: [
          { required: true, message: '目录不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async fetchList () {
      if (!this.$route.query.id) {
        const { data } = await list()
        this.tableData = data.items
        this.shujuleix = data.items
        this.total = data.counts
        console.log(data)
      } else {
        this.jumpid.subjectID = this.$route.query.id
        const { data } = await list(this.jumpid)
        this.tableData = data.items
        this.shujuleix = data.items
        this.total = data.counts
        console.log(data)
      }
    },
    async currentChange (page) {
      this.params.page = page
      const { data } = await list({ ...this.params, ...this.formData })
      this.total = data.counts
      this.tableData = data.items
      console.log(this.params, this.formData)
    },
    async sizeChange (page) {
      this.params.pagesize = page
      const { data } = await list({ ...this.params, ...this.formData })
      this.total = data.counts
      this.tableData = data.items
      console.log(this.params, this.formData)
    },
    async changeState (row) {
      await changeState({ id: row.id, state: `${row.state === 1 ? 0 : 1}` })
      this.$message.success('操作成功')
      this.fetchList()
    },
    formatstate (value) {
      const state = {
        1: '已启用',
        0: '已禁用'
      }
      return state[value]
    },
    indexMethod (index) {
      return index * 1 + 1
    },
    addLabel () {
      if (!this.$route.query.id) {
        this.showdialog = true
        this.reviselables = {
          subjectID: '',
          tagName: ''
        }
        this.detailid = ''
      } else {
        this.showdialog = true
        this.reviselables.subjectID = this.$route.query.id
        this.reviselables.tagName = ''
        this.detailid = ''
      }
    },
    async reviselable (id) {
      const { data } = await list()
      const newDate = data.items.filter(res => res.id === id)
      this.detailid = id
      this.reviselables.subjectID = newDate[0].subjectID
      this.reviselables.tagName = newDate[0].tagName
      this.showdialog = true
    },
    closeDialog () {
      this.showdialog = false
      this.fetchList()
    },
    async search () {
      this.params.page = 1
      const { data } = await list({ ...this.params, ...this.formData })
      console.log(data)
      this.tableData = data.items
      this.total = data.counts
      console.log({ ...this.params, ...this.formData })
    },
    async purge () {
      this.formData = {
        state: null,
        tagName: ''
      }
    },
    async dellable (row) {
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        console.log(row)
        this.fetchList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang="less">
.el-table  {
  margin-top: 20px;
}
.el-pagination {
  margin-top: 20px;
}
.dom {
  margin: 0 10px;
}
.sta {
  margin-left: 40px;
  margin-right: 10px;
}
.block {
  float: right;
  margin-bottom: 20px;
}
 /deep/ .el-alert__icon.is-big {
    font-size: 18px;
    width: 28px;
}
/deep/ .alert {
  text-align: center;
}
</style>
